<!--
 * @Author: Songjq
 * @Date: 2022-01-24 11:28:21
 * @Desscription: 
-->
<template>
  <!--表单渲染-->
  <el-dialog
    :close-on-click-modal="false"
    destroy-on-close
    :title="isEdit?'编辑':'新增'"
    :visible.sync="dialogVisible"
    width="660px"
    :before-close="resetForm"
  >
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item
        label="公告标题"
        prop="noticeTitle"
      >
        <el-input
          size="small"
          v-model="ruleForm.noticeTitle"
          placeholder="请输入公告标题"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="公告类型"
        prop="noticeType"
      >
        <el-select
          v-model="ruleForm.noticeType"
          placeholder="请选择公告类型"
          class="w-100"
        >
          <el-option
            v-for="item in dict.sys_notice_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="状态"
        prop="status"
      >
        <template v-for="(item,idx) in dict.sys_disabled_status">
          <el-radio
            :key="idx"
            v-model="ruleForm.status"
            :label="item.value"
          >{{item.label}}</el-radio>
        </template>
      </el-form-item>
      <el-form-item
        label="内容"
        prop="noticeContent"
      >
        <editor
          ref="editor"
          @change="setNoticeContent"
          :isMenu="false"
        />
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="resetForm()">取 消</el-button>
      <el-button
        type="primary"
        @click="submit()"
        :loading="btnLoading"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { postNotice } from "@http/information/notice.js";
import Editor from "../../components/Editor.vue";
export default {
  components: { Editor },
  name: "notice",
  props: ["isEdit"],
  dicts: ["sys_disabled_status", "sys_notice_type"],
  data() {
    return {
      ruleForm: {
        noticeTitle: "",
        noticeType: "",
        status: "",
        noticeContent: "",
      },
      rules: {
        noticeTitle: [
          { required: true, message: "请输入公告标题", trigger: "blur" },
        ],
        noticeType: [
          { required: true, message: "请选择公告类型", trigger: "change" },
        ],
        noticeContent: [
          { required: true, message: "请输入内容", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      dialogVisible: false,
      btnLoading: false,
    };
  },
  methods: {
    init(row) {
      if (row) {
        this.ruleForm = row;
      }
      this.$refs.editor.init(this.ruleForm.noticeContent);
    },

    // 设置公告内容
    setNoticeContent(html) {
      this.ruleForm.noticeContent = html;
    },

    // 新增提交
    submit() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          postNotice(this.ruleForm).then((res) => {
            this.dialogVisible = false;
            this.$message.success("操作成功");
            this.resetForm();
            this.$emit("init");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    resetForm() {
      this.dialogVisible = false;
      this.btnLoading = false;
      this.ruleForm = {
        noticeTitle: "",
        noticeType: "",
        status: "",
        noticeContent: "",
      };
    },
  },
};
</script>
<style lang="scss"></style>